package com.hc.model.code03.test8;

import javax.swing.*;
import java.util.HashMap;

/**
 * @version v1.0
 * @ClassName: BoxFactory
 * @Description: 工厂类（单例）
 * @Author: HC
 */
public class BoxFactory {
    private HashMap<String, AbstractBox> map;

    // 将共享的部分存储在一个单一容器中
    private BoxFactory() {
        map = new HashMap<>();
        map.put("I", new IBox());
        map.put("L", new LBox());
        map.put("O", new OBox());
    }

    private static volatile BoxFactory boxFactory;

    public static BoxFactory getInstance() {
        if (boxFactory == null) {
            synchronized (BoxFactory.class) {
                if (boxFactory == null) {
                    boxFactory = new BoxFactory();
                }
            }
        }
        return boxFactory;
    }

    // 根据名称获取图形对象
    public AbstractBox getShape(String name) {
        return map.get(name);
    }
}
